package com.shs.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.bus.entity.Customer;
import com.shs.bus.entity.Provider;
import com.shs.bus.service.ProviderService;
import com.shs.bus.vo.CustomerVo;
import com.shs.bus.vo.ProviderVo;
import com.shs.sys.common.Constants;
import com.shs.sys.common.DataGridView;
import com.shs.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  供应商前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-16
 */
@RestController
@RequestMapping("provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    /**
     * 查询供应商
     * @return
     */
    @RequestMapping("loadAllProvider")
    public DataGridView loadAllProvider(ProviderVo vo){
        Page<Provider> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Provider> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(vo.getProvidername()),"providername",vo.getProvidername());
        wrapper.like(StringUtils.isNotBlank(vo.getTelephone()),"telephone",vo.getTelephone());
        wrapper.like(StringUtils.isNotBlank(vo.getConnectionperson()),"connectionperson",vo.getConnectionperson());
        wrapper.orderByDesc("id");
        providerService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());

    }

    /**
     * 添加供应商
     * @param vo
     * @return
     */
    @RequestMapping("addProvider")
    public ResultObj addProvider(ProviderVo vo){
        try {
            providerService.save(vo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改供应商
     * @param vo
     * @return
     */
    @RequestMapping("updateProvider")
    public ResultObj updateProvider(ProviderVo vo){
        try {
            providerService.updateById(vo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("deleteProvider")
    public ResultObj deleteProvider(Integer id){
        try {
            providerService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param vo
     * @return
     */
    @RequestMapping("batchDeleteProvider")
    public ResultObj batchDeleteProvider(ProviderVo vo){
        List<Integer> idsList = new ArrayList<>();
        try {
            for (Integer id : vo.getIds()) {
                idsList.add(id);
            }
            providerService.removeByIds(idsList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     *加载查询条件供应商的下拉列表
     */
    @RequestMapping("loadAllProviderForSelect")
    public DataGridView loadAllProviderForSelect(){
        QueryWrapper<Provider> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available",Constants.AVAILABLE_TRUE);
        List<Provider> list = providerService.list(queryWrapper);
        return new DataGridView(list);
    }

}
